const json = require('../../package.json')
export const isDevice = () => {
    let ua = navigator.userAgent.toLowerCase();
    if (ua.indexOf('micromessenger') !== -1) return 'isWechat';
    if (ua.indexOf('android') !== -1) return 'isAndroid';
    if ((ua.indexOf('iphone') !== -1) || (ua.indexOf('ipad') !== -1)) return 'iphone';
    if (/macintosh|mac os x/i.test(navigator.userAgent)) return 'isMac';
    if (/windows|win32/i.test(navigator.userAgent)) return 'isWindows';
}
export const getImagePrefix = () => {
    if (process.env.NODE_ENV === 'production') {
        return json.url.production
    }
    return json.url.devlopment;
}

export const replaceImage = (imageArray: any[]) => {
    if (!imageArray || imageArray.length === 0) return [];
    for (let i = 0; i < imageArray.length; i++) {
        if (imageArray[i].image.indexOf(getImagePrefix()) > -1) {
            imageArray[i].image = imageArray[i].image.split(getImagePrefix())[1]
        }
    }
    return imageArray;
}

export const getErrorFadeBack = () => ""
export const getUserRoles = (type: string) => {
    switch (type) {
        case 'sysadmin':
            return '管理员';
        case 'butler':
            return '管家';
        case 'owner':
            return '业主';
        case 'customer':
            return '客户';
        default:
            return '客户';
    }
}
export const getUserPosition = (userAddr: any, userDistrict: any, data: any) => {

}

export const getLocationOrigin = () => window.location.protocol + "//" + window.location.host

export const findAllParents = (data: any, id: any) => {
    // 深度遍历查找
    function dfs(data: any, id: any, parents: any) {
        for (let i = 0; i < data.length; i++) {
            let item = data[i];
            // 找到id则返回父级id
            if (item.value === id) return parents;
            // children不存在或为空则不递归
            if (!item.children || !item.children.length) continue;
            // 往下查找时将当前id入栈
            parents.push(item.value);

            if (dfs(item.children, id, parents).length) return parents;
            // 深度遍历查找未找到时当前id 出栈
            parents.pop()
        }
        // 未找到时返回空数组
        return [];
    }

    return dfs(data, id, []);
}
export const findAllParentsName = (data: any, id: any) => {
    // 深度遍历查找
    function dfs(data: any, id: any, parents: any) {
        for (let i = 0; i < data.length; i++) {
            let item = data[i];
            // 找到id则返回父级id
            if (item.value === id) return parents;
            // children不存在或为空则不递归
            if (!item.children || !item.children.length) continue;
            // 往下查找时将当前id入栈
            parents.push(item);

            if (dfs(item.children, id, parents).length) return parents;
            // 深度遍历查找未找到时当前id 出栈
            parents.pop()
        }
        // 未找到时返回空数组
        return [];
    }

    return dfs(data, id, []);
}
// 答应日志
export const Logging = (value?: any, title?: string) => {
    if (!value || process.env.NODE_ENV === 'production') return;
    let str = title ? `====LOG===${title}====>` : `====LOG====>`;
    console.log(str, value);
}
